package com.cdy.jxc_project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdy.jxc_project.domain.TCustomer;
import com.cdy.jxc_project.domain.vo.TCustomerVo;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.service.TCustomerService;
import com.cdy.jxc_project.utils.AddSysLog;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/20 16:40
 * */
@RestController
@RequestMapping("/tCustomer")
public class TCustomerController {

    @Resource
    private TCustomerService tCustomerService;
    @AddSysLog
    @PostMapping("/queryAll")
    public R tCustomerList(@RequestBody TCustomerVo tCustomerVo){

        Page<TCustomer> tCustomerPage = tCustomerService.queryTCustomerPage(tCustomerVo);

        return R.ok().message("查询成功").data("tCustomerList",tCustomerPage);
    }
    @DeleteMapping("/deleteTGoodsUnit/{id}")
    public R deleteTCustomer(@PathVariable Integer id){
        boolean b = tCustomerService.deleteTCustomer(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("错误");
        }

    }
    @AddSysLog
    @PostMapping("/insertAndUpdateTCustomer")
    public R insertAndUpdateTCustomer(@RequestBody TCustomer tCustomer){
        try{
            if (tCustomer.getId()==null){
                tCustomerService.insertTCustomer(tCustomer);
                return R.ok().message("保存成功");
            }else {
                tCustomerService.updateTCustomer(tCustomer);
                return R.ok().message("修改成功");
            }

        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("程序出错");
        }
    }
}
